package com.bishe.cyh.service.Impl;

import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.toolkit.ImageFactory;
import com.arcsoft.face.toolkit.ImageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bishe.cyh.mapper.LoginAddMapper;
import com.bishe.cyh.mapper.LoginMapper;
import com.bishe.cyh.mode.*;
import com.bishe.cyh.service.FaceEngineService;
import com.bishe.cyh.service.LoginService;
import com.bishe.cyh.utils.Base64Util;
import com.bishe.cyh.utils.SnowIdUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @Author: Poppin
 * @Date: 2022/3/3 18:05
 * @Version: 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    private FaceEngineService faceEngineService;
    private String[] REerroe = {"请右转头","请左转头","请眨眨眼","请张张嘴"};

    @Value("${config.imagepath.path}")
    public String Path;
    public List<LoginFace> loginFaceList = new ArrayList<>();
    private LoginMapper loginMapper;
    private LoginAddMapper loginAddMapper;

    @Autowired
    public void setFaceEngineService(FaceEngineServiceImpl faceEngineService,LoginMapper loginMapper,LoginAddMapper loginAddMapper){
        this.faceEngineService=faceEngineService;
        this.loginMapper=loginMapper;
        this.loginAddMapper = loginAddMapper;
    }

    public void initFace(){
        loginFaceList = loginMapper.selectList(null);
    }

    @Override
    public Result FaceReco(MultipartFile file) throws Exception {
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."),oldName.length());
        File dest = new File(Path + newName);
        file.transferTo(dest);
        ImageInfo rgbData = ImageFactory.getRGBData(new File(Path+newName));
        List<FaceInfo> faceInfoList = faceEngineService.detectFaces(rgbData);
        if (CollectionUtils.isNotEmpty(faceInfoList)){
            byte[] feature = faceEngineService.extractFaceFeature(rgbData, faceInfoList.get(0));
            if (feature!=null){
                return new Result<Imageinfo>(0,"成功",new Imageinfo(Path+newName,"人脸检测成功可以使用"));
            }else {
                return new Result<Messqge>(-1,"失败",new Messqge("人脸检测失败请更换图片"));
            }
        }else {
            return new Result<Messqge>(-1,"失败",new Messqge("人脸检测失败请更换图片"));
        }

    }

    @Override
    public Result FaceAdd(Loginfaceadd loginfaceadd) throws Exception {
        ImageInfo rgbData = ImageFactory.getRGBData(new File(loginfaceadd.getBase64()));
        List<FaceInfo> faceInfoList = faceEngineService.detectFaces(rgbData);
        if (CollectionUtils.isNotEmpty(faceInfoList)){
            byte[] feature = faceEngineService.extractFaceFeature(rgbData, faceInfoList.get(0));
            LoginAdd loginAdd = new LoginAdd();
            loginAdd.setSnowid(SnowIdUtils.uniqueLongHex());
            loginAdd.setName(loginfaceadd.getName());
            loginAdd.setFaceFeature(feature);
            loginAdd.setYgid(loginfaceadd.getYgid());
            int count = loginAddMapper.insert(loginAdd);
            if (count==1){
                initFace();
                return new Result<Messqge>(0,"成功",new Messqge("添加成功"));
            }else {
                return new Result<Messqge>(-1,"失败",new Messqge("添加失败请重新尝试"));
            }
        }else {
            return new Result<Messqge>(-1,"失败",new Messqge("人脸检测失败请重新上传"));
        }
    }

    @Override
    public Result Getyginfo() {
        List<YGinfo> list = loginAddMapper.findAllyg();
        return new Result(0,"成功",list);
    }

    @Override
    public Result DELETE(Integer ygid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ygid",ygid);
        int count = loginAddMapper.delete(queryWrapper);
        if (count!=0){
            return new Result<Messqge>(0,"成功",new Messqge("删除成功"));
        }else {
            return new Result<Messqge>(-1,"失败",new Messqge("删除失败"));
        }
    }

    @Override
    public Result Login(byte[] image) throws Exception {

        ImageInfo rgbData = ImageFactory.getRGBData(image);
        List<FaceInfo> faceInfoList = faceEngineService.detectFaces(rgbData);
        if (CollectionUtils.isNotEmpty(faceInfoList)){
            for (FaceInfo faceInfo:faceInfoList){
                byte[] feature = faceEngineService.extractFaceFeature(rgbData,faceInfo);
                if (feature!=null){
                   for (LoginFace loginFace:loginFaceList){
                       if (faceEngineService.compare(feature, loginFace.getFaceFeature())>0.5){
                           return new Result<LoginInfo>(0,"成功",new LoginInfo(loginFace.getName(), "123456"));
                       }
                   }
                }else {
                    Random random = new Random();
                    return new Result<Messqge>(-2,"失败",new Messqge(REerroe[random.nextInt(4)]));
                }
            }

        }else {
            return new Result<Messqge>(-1,"失败",new Messqge("请正对摄像头"));
        }
        return new Result<Messqge>(-1,"失败",new Messqge("请正对摄像头"));
    }
}
